//
//  HX_CycleImage.m
//  UI16-UICollectionViewCycleImage
//
//  Created by 韩旭 on 16/8/4.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "HX_CycleImage.h"
#define kCollectionView @"collectionView"
@interface HX_CycleImage ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UIPageControl *pageControll;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@end
@implementation HX_CycleImage

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor redColor];
        [self setUp];
    }
    return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self =  [super initWithCoder:aDecoder];
    if (self) {
        self.backgroundColor = [UIColor redColor];
        [self setUp];
    }
    return self;
}
- (void)setUp {
    [self creatCollectionView];
}
- (void)creatCollectionView {
    _layout = [[UICollectionViewFlowLayout alloc] init];
    _layout.scrollDirection = 1;
    _layout.minimumInteritemSpacing = 0;
    _layout.minimumLineSpacing = 0;
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [self addSubview:_collectionView];
    _collectionView.backgroundColor = [UIColor greenColor];
    _collectionView.showsHorizontalScrollIndicator = YES;
    _collectionView.pagingEnabled = YES;
    [_collectionView registerClass:[HX_CycleImageCell class] forCellWithReuseIdentifier:kCollectionView];
    
    
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _arrDataSource.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HX_CycleImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionView forIndexPath:indexPath];
    cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", _arrDataSource[indexPath.item]]];
    return cell;
}
- (void)layoutSubviews {
    [super layoutSubviews];
    _collectionView.frame = self.bounds;
    _layout.itemSize = self.bounds.size;
    [_collectionView setContentOffset:CGPointMake(self.bounds.size.width, 0)];
}


- (void)changeCollectionView {
    //    直接判断与0相等有何问题
    if (_collectionView.contentOffset.x <= 0) {
        //        [_collectionView setContentOffset:CGPointMake(_collectionView.bounds.size.width * (_arrDataSource.count - 2), 0)];
        [_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:_arrDataSource.count - 2 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
    } else if (_collectionView.contentOffset.x == _collectionView.bounds.size.width * (_arrDataSource.count - 1)){
        [_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:1 inSection:0] atScrollPosition:0 animated:NO];
    }

}

- (void)setArrDataSource:(NSArray *)arrDataSource {
    _arrDataSource = arrDataSource;
    [_collectionView reloadData];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
////    直接判断与0相等有何问题
//    if (scrollView.contentOffset.x <= 0) {
////        [_collectionView setContentOffset:CGPointMake(_collectionView.bounds.size.width * (_arrDataSource.count - 2), 0)];
//        [_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:_arrDataSource.count - 2 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
//    } else if (scrollView.contentOffset.x == _collectionView.bounds.size.width * (_arrDataSource.count - 1)){
//        [_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:1 inSection:0] atScrollPosition:0 animated:NO];
//    }
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end

@implementation HX_CycleImageCell
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _imageView = [[UIImageView alloc] initWithFrame:self.bounds];
        [self.contentView addSubview:_imageView];
    }
    return self;
}


@end

